#!/usr/bin/ruby
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
# frozen_string_literal: true

require 'sinatra'

CCI_SRC = ENV['CCI_SRC'] || '/c/compass-ci'
require "#{CCI_SRC}/src/lib/web_backend.rb"

set :bind, '0.0.0.0'
set :port, 32767
set :show_exceptions, false
set :environment, :production

# GET /compare_candidates
# Response:
# - {"query_conditions": {...}, "dimension": [...]}
get '/compare_candidates' do
  compare_candidates
end

# GET /compare?suite=borrow&dimension=tbox_group
# must:
# - query_conditions
#   - os / os_version / suite / os_arch / tbox_group
# - dimension
#   - os / os_version / os_arch / suite / tbox_group
#
# Response:
#   - compare result string / "No Data." / "No Difference."
get '/compare' do
  compare(params)
end

# GET /get_jobs?upstream_repo=a&page_size=12&start_date=2020-11-03&end_date=2020-11-03
# optional:
# - upstream_repo
#   - regex search
# - os
# - os_version
# - os_arch
# - suite
# - category
# - testbox
# - job_state
# - id
# - start_date
# - end_date
# - page_size
#   - default: 20
# - page_num
#   - default: 1
#
# Response:
#   - default: 20 jobs
get '/get_jobs' do
  get_jobs(params)
end

# GET /get_repos?git_repo=a&page_size=12&page_num=3
# optional:
# - git_repo
# - page_size
#   - default: 20
# - page_num
#   - default: 1
#
# Response:
#   - default: 20 repos
get '/get_repos' do
  get_repos(params)
end

# POST /compare_template
# - header: "Content-Type: Application/json"
# - body: json
#   eg:
#     {
#       "compare_metrics": ["fio.write_iops", "fio.read_iops"],
#       "filter":{
#           "suite": ["fio-basic"],
#           "os_arch": ["aarch64", "x86"]
#         },
#       "compare_dimensions": [
#           {
#             "os": "debian",
#             "os_version": "sid"
#           },
#           {
#             "os": "openeuler",
#             "os_version: 20.03
#           }
#         ],
#       "x_params": ["bs", "test_size"],
#       "title": "Hackbench Performance Testing",
#       "unit": "KB/s"
#     }
#
# Response:
#   - compare result
post '/compare_template' do
  request.body.rewind # in case someone already read it
  compare_template(request.body.read)
end

# GET /get_testboxes
# Response:
#   - { total: total, testboxes[ 'testbox1', 'testbox2', ... ] }
get '/get_testboxes' do
  query_testboxes
end

# GET /get_tbox_state?testbox=$testbox
# Response:
#   - {
#       testbox: $testbox,
#       states: {
#         "state": "$current_state",
#         "job_id": "$current_job_id",
#         "history": [
#           {
#             "time": "$history_time",
#             "mac": "$mac",
#             "ip": "$ip",
#             "job_id": "$history_job_id",
#             "state": "$history_state"
#           },
#           ...
#         ]
#       }
#     }
get '/get_tbox_state' do
  get_tbox_state(params)
end

# GET /get_repo_statistics?git_repo=$git_repo
# Response:
#   - {
#       "title": "new refs statistics",
#       "unit": "times",
#       "x_name": "date",
#       "source": [["x_params", $date1, ..., $date_today],
#                  ["new_ref_times", $times_1, ..., $times_today]]
#     }
get '/get_repo_statistics' do
  new_refs_statistics(params)
end
